package cn.action.photogroup;

import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.apache.struts2.ServletActionContext;
import cn.action.BaseAction;
import cn.beans.Allfile;
import cn.beans.NewsCategory;
import cn.beans.Photo;
import cn.beans.Photogroup;
import cn.beans.Product;
import cn.beans.ProductCategory;
import cn.beans.ProductFile;
import cn.busiBean.ProductCategoryBean;
import cn.utils.DateFormat;
import cn.utils.ImportExcelUtil;
import cn.utils.PageModel;

import com.opensymphony.xwork2.ActionContext;

public class PhotogroupAction extends BaseAction {

	private File file;
	private Photogroup pg;
	private String booksId;
	private String errorinfo;
	private String testing;
	private String rows;
	private String groupId;
	private String groupTopId;
	private String act;
	private String groupName;
	private String qishu;
	private String keywords;
	private String type;
	private List list;
	private List listpic;//图片列表
	private int totalqishu;
	private int currentPage=1;
	private int pageSize=15;
	private PageModel pm = new PageModel();
	private String preid;
	private String nextid;
	private String pretitle;
	private String nexttitle;

	/**
	 * 导入bo excel
	 * 参数：
	 * @return
	 */
	public String saveBoExcel()throws Exception {

		String sadminId = (String)ActionContext.getContext().getSession().get("adminId");
		
		String sadminName = (String)ActionContext.getContext().getSession().get("adminName");		
		
		if(null == sadminId){
			errorinfo = "Time over";
			return LOGIN;
		}else{
		String configType = "Photogroup_bo.xml";
		
		ImportExcelUtil importExcelUtil=new ImportExcelUtil();
		
		Map session=ServletActionContext.getContext().getSession();
		
		List<Photogroup> listC=importExcelUtil.getExcelObjectList(configType,file,session);
		
		String photoPath = ServletActionContext.getServletContext().getRealPath("/upload/bo");
		
		String picerror = "";
		String picerrorlog = "";
		File filetest = null;
		if(null != listC && !listC.isEmpty()){
			for(int i =0 ;i<listC.size();i++){
				listC.get(i).setGroupIcon(listC.get(i).getLable2()+"_"+1+".jpg");//设置默认图片
				listC.get(i).setKind("bo");
				listC.get(i).setCreDate(new Date());
				listC.get(i).setCreUserId(sadminId);
				listC.get(i).setCreName(sadminName);
				listC.get(i).setLable19(0);//浏览量
			}
			if(testing.equals("0")){
				super.commonservice.saveOrUpdateAll(listC);
			}
			
			Allfile p = null;
			List<Allfile> listAllfile = new ArrayList();
			int j = 0;
			String imgName = "";
			String extent = "";
			int pictures =0;
			for(int i =0;i<listC.size();i++){
			//	String productId = listC.get(i).getId();
				pictures = listC.get(i).getLable20(); //取得图片总数
				for(j = 1; j<pictures+1;j++){
				imgName = listC.get(i).getLable2()+"_"+j+".jpg";
				extent= ".jpg";
				int size = 0;
				File file =new File( photoPath+"\\"+imgName);
				if(null != file && file.length() != 0){
					//System.out.println("size:"+file.length());
					size = Integer.parseInt(String.valueOf(file.length()));
					if(imgName.indexOf("—")>0){
					picerror = picerror+"<p>"+listC.get(i).getLable1()+" "+listC.get(i).getLable2()+"的图片"+imgName+"含有中文连字符—，请修改该编号，改成英文连字符，并将图片重命名;</p>";
					}
				}else{
					picerror = picerror+"<p>"+listC.get(i).getLable1()+" "+listC.get(i).getLable2()+"的图片"+imgName+"未找到;</p>";
					picerrorlog = picerrorlog+listC.get(i).getLable1()+" "+listC.get(i).getLable2()+"的图片"+imgName+"未找到;";
				}
				p = new Allfile();
				p.setParentId(listC.get(i).getId());
				p.setExtName(extent);
				p.setFileName(imgName);
				p.setSaveName(imgName);
				p.setFileSize(size);
				p.setTurn(j);
				p.setUploadDate(new Date());
				listAllfile.add(p);
				}
			}
			
			if(!listAllfile.isEmpty() && null != listAllfile){
				if(testing.equals("0")){
				super.commonservice.saveOrUpdateAll(listAllfile);
				}
			}
		}
		if(testing.equals("0")){//导入数据
		if(picerror.length()>1){
			rows = "<div style='color:#ff0000;'>已成功导入"+listC.size()+"行数据。</div><div style='color:#ff0000;'><br/><p>警告：</p>"+picerror+"</div>";
		}else{
			rows = "<div style='color:#ff0000;'>已成功导入"+listC.size()+"行数据。</div>";
		}
		//保存导入数据历史
		String importlogPath = ServletActionContext.getServletContext().getRealPath("/upload/bo");
		   FileWriter fw = new FileWriter(importlogPath+"/BO导入日志_"+DateFormat.getDateId()+".txt");
		   String[] lines = picerrorlog.split(";");
		   BufferedWriter bw = new BufferedWriter(fw);
		   bw.write("BO导入日志\r\n\r\n");
		   bw.write("操作员："+(String)ActionContext.getContext().getSession().get("adminId")+"\r\n");
		   SimpleDateFormat timetemp=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	       String timeStr=timetemp.format(new Date());
		   bw.write("时　间："+timeStr+"\r\n\r\n");
		   
		   bw.write("结　果：\r\n");
		   bw.write("　　成功导入"+listC.size()+"行数据。\r\n");
		   
		   if(lines.length>0){
		   bw.write("\r\n警　告：\r\n");
		   for (int i = 0; i < lines.length; i++) {
		    bw.write((i+1)+"、"+lines[i] + "\r\n");
		   }
		   }else{
			   bw.write("\r\n无异常数据\r\n");
		   }
		   bw.close();
		}else{//测试数据
			if(picerror.length()>1){
				rows = "<div style='color:#ff0000;'>成功找到"+listC.size()+"行数据。</div><div style='color:#ff0000;'><br/><p>警告：</p>"+picerror+"</div>";
			}else{
				rows = "<div style='color:#ff0000;'>成功找到"+listC.size()+"行数据。</div><div style='color:#ff0000;'><br/><p>恭喜您，您的数据符合导入规则，并且能匹配上所有图片。</p>"+picerror+"</div>";
			}
			//保存导入数据历史
			String importlogPath = ServletActionContext.getServletContext().getRealPath("/upload/bo");
			   FileWriter fw = new FileWriter(importlogPath+"/BO导入日志_"+DateFormat.getDateId()+".txt");
			   String[] lines = picerrorlog.split(";");
			   BufferedWriter bw = new BufferedWriter(fw);
			   bw.write("BO测试导入日志\r\n\r\n");
			   bw.write("操作员："+(String)ActionContext.getContext().getSession().get("adminId")+"\r\n");
			   SimpleDateFormat timetemp=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		       String timeStr=timetemp.format(new Date());
			   bw.write("时　间："+timeStr+"\r\n\r\n");
			   
			   bw.write("结　果：\r\n");
			   bw.write("　　成功找到"+listC.size()+"行数据。\r\n");
			   
			   if(lines.length>0){
			   bw.write("\r\n警　告：\r\n");
			   for (int i = 0; i < lines.length; i++) {
			    bw.write((i+1)+"、"+lines[i] + "\r\n");
			   }
			   }else{
				   bw.write("\r\n无异常数据\r\n");
			   }
			   bw.close();
		}

		return SUCCESS;
		}
	}
	
	/**
	 * 今日礼品  excel
	 * 参数：
	 * @return
	 */
	public String saveReportExcel()throws Exception {

		String sadminId = (String)ActionContext.getContext().getSession().get("adminId");
		
		String sadminName = (String)ActionContext.getContext().getSession().get("adminName");		
		
		if(null == sadminId){
			errorinfo = "Time over";
			return LOGIN;
		}else{
		String configType = "Photogroup_report.xml";
		
		ImportExcelUtil importExcelUtil=new ImportExcelUtil();
		
		Map session=ServletActionContext.getContext().getSession();
		
		List<Photogroup> listC=importExcelUtil.getExcelObjectList(configType,file,session);
		
		String photoPath = ServletActionContext.getServletContext().getRealPath("/upload/report");
		
		String picerror = "";
		String picerrorlog = "";
		File filetest = null;
		String groupTopName = "";
		if(null != listC && !listC.isEmpty()){
			for(int i =0 ;i<listC.size();i++){
				if(listC.get(i).getGroupTopId().equals("2")){
					File file =new File( photoPath+"\\"+listC.get(i).getLable2()+"_1.jpg");
					if(null != file && file.length() != 0){
						if(listC.get(i).getLable2().indexOf("—")>0){
						picerror = picerror+"<p>"+listC.get(i).getLable1()+" "+listC.get(i).getLable2()+"的图片"+listC.get(i).getLable2()+".jpg"+"含有中文连字符—，请修改该编号，改成英文连字符，并将图片重命名;</p>";
						}
					}else{
						picerror = picerror+"<p>"+listC.get(i).getLable1()+" "+listC.get(i).getLable2()+"的图片"+listC.get(i).getLable2()+".jpg"+"未找到;</p>";
						picerrorlog = picerrorlog+listC.get(i).getLable1()+" "+listC.get(i).getLable2()+"的图片"+listC.get(i).getLable2()+".jpg"+"未找到;";
					}
					listC.get(i).setGroupIcon(listC.get(i).getLable2()+"_1.jpg");//设置默认图片
				}
				//groupName = "今日礼品　第"+listC.get(i).getGroupId()+"期";
				//
				listC.get(i).setGroupName(listC.get(i).getGroupId());
				if(listC.get(i).getGroupTopId().equals("1")){
					groupTopName = "时尚追踪";
				}else if(listC.get(i).getGroupTopId().equals("2")){
					groupTopName = "市场动态调查";
				}else if(listC.get(i).getGroupTopId().equals("3")){
					groupTopName = "流行趋势";
				}else if(listC.get(i).getGroupTopId().equals("4")){
					groupTopName = "工艺分享";
				}else if(listC.get(i).getGroupTopId().equals("5")){
					groupTopName = "新品推荐";
				}else{
					testing = "1";
					picerror = "<p>第"+i+"行分类异常;</p>";
					picerrorlog = picerrorlog+"<p>第"+i+"行分类异常;</p>";
				}
				listC.get(i).setGroupTopName(groupTopName);
				listC.get(i).setKind("book");
				listC.get(i).setCreDate(new Date());
				listC.get(i).setCreUserId(sadminId);
				listC.get(i).setCreName(sadminName);
				listC.get(i).setLable19(0);//浏览量
				listC.get(i).setTurn(i);
			}
			if(testing.equals("0")){
				super.commonservice.saveOrUpdateAll(listC);
			}
			
			Allfile p = null;
			List<Allfile> listAllfile = new ArrayList();
			int j = 0;
			String imgName = "";
			String extent = "";
			int pictures =0;
			for(int i =0;i<listC.size();i++){
				//if(!listC.get(i).getGroupTopId().equals("2")){
				pictures = listC.get(i).getLable20(); //取得图片总数
				for(j = 1; j<pictures+1;j++){
					imgName = listC.get(i).getLable2()+"_"+j+".jpg";
					extent= ".jpg";
					int size = 0;
					File file =new File( photoPath+"\\"+imgName);
					if(null != file && file.length() != 0){
						//System.out.println("size:"+file.length());
						size = Integer.parseInt(String.valueOf(file.length()));
						if(imgName.indexOf("—")>0){
						picerror = picerror+"<p>"+listC.get(i).getLable1()+" "+listC.get(i).getLable2()+"的图片"+imgName+"含有中文连字符—，请修改该编号，改成英文连字符，并将图片重命名;</p>";
						}
					}else{
						picerror = picerror+"<p>"+listC.get(i).getLable1()+" "+listC.get(i).getLable2()+"的图片"+imgName+"未找到;</p>";
						picerrorlog = picerrorlog+listC.get(i).getLable1()+" "+listC.get(i).getLable2()+"的图片"+imgName+"未找到;";
					}
					p = new Allfile();
					p.setParentId(listC.get(i).getId());
					p.setExtName(extent);
					p.setFileName(imgName);
					p.setSaveName(imgName);
					p.setFileSize(size);
					p.setTurn(j);
					p.setUploadDate(new Date());
					listAllfile.add(p);
					}
				//}
			
			if(!listAllfile.isEmpty() && null != listAllfile){
				if(testing.equals("0")){
				super.commonservice.saveOrUpdateAll(listAllfile);
				}
			}
			
			}
			
		}
		if(testing.equals("0")){//导入数据
		if(picerror.length()>1){
			rows = "<div style='color:#ff0000;'>已成功导入"+listC.size()+"行数据。</div><div style='color:#ff0000;'><br/><p>警告：</p>"+picerror+"</div>";
		}else{
			rows = "<div style='color:#ff0000;'>已成功导入"+listC.size()+"行数据。</div>";
		}
		//保存导入数据历史
		String importlogPath = ServletActionContext.getServletContext().getRealPath("/upload/report");
		   FileWriter fw = new FileWriter(importlogPath+"/今日礼品导入日志_"+DateFormat.getDateId()+".txt");
		   String[] lines = picerrorlog.split(";");
		   BufferedWriter bw = new BufferedWriter(fw);
		   bw.write("今日礼品导入日志\r\n\r\n");
		   bw.write("操作员："+(String)ActionContext.getContext().getSession().get("adminId")+"\r\n");
		   SimpleDateFormat timetemp=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	       String timeStr=timetemp.format(new Date());
		   bw.write("时　间："+timeStr+"\r\n\r\n");
		   
		   bw.write("结　果：\r\n");
		   bw.write("　　成功导入"+listC.size()+"行数据。\r\n");
		   
		   if(lines.length>0){
		   bw.write("\r\n警　告：\r\n");
		   for (int i = 0; i < lines.length; i++) {
		    bw.write((i+1)+"、"+lines[i] + "\r\n");
		   }
		   }else{
			   bw.write("\r\n无异常数据\r\n");
		   }
		   bw.close();
		}else{//测试数据
			if(picerror.length()>1){
				rows = "<div style='color:#ff0000;'>成功找到"+listC.size()+"行数据。</div><div style='color:#ff0000;'><br/><p>警告：</p>"+picerror+"</div>";
			}else{
				rows = "<div style='color:#ff0000;'>成功找到"+listC.size()+"行数据。</div><div style='color:#ff0000;'><br/><p>恭喜您，您的数据符合导入规则，并且能匹配上所有图片。</p>"+picerror+"</div>";
			}
			//保存导入数据历史
			String importlogPath = ServletActionContext.getServletContext().getRealPath("/upload/report");
			   FileWriter fw = new FileWriter(importlogPath+"/今日礼品导入日志_"+DateFormat.getDateId()+".txt");
			   String[] lines = picerrorlog.split(";");
			   BufferedWriter bw = new BufferedWriter(fw);
			   bw.write("今日礼品测试导入日志\r\n\r\n");
			   bw.write("操作员："+(String)ActionContext.getContext().getSession().get("adminId")+"\r\n");
			   SimpleDateFormat timetemp=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		       String timeStr=timetemp.format(new Date());
			   bw.write("时　间："+timeStr+"\r\n\r\n");
			   
			   bw.write("结　果：\r\n");
			   bw.write("　　成功找到"+listC.size()+"行数据。\r\n");
			   
			   if(lines.length>0){
			   bw.write("\r\n警　告：\r\n");
			   for (int i = 0; i < lines.length; i++) {
			    bw.write((i+1)+"、"+lines[i] + "\r\n");
			   }
			   }else{
				   bw.write("\r\n无异常数据\r\n");
			   }
			   bw.close();
		}

		return SUCCESS;
		}
	}
	
	
	/**
	 * 导出 bo excel文档模板
	 * @return
	 */
	public InputStream getBoexcel(){
		
		ActionContext act= ServletActionContext.getContext();
		InputStream in=null;
		try{
			
			ByteArrayOutputStream ost=new ByteArrayOutputStream();
			WritableWorkbook  wwb = Workbook.createWorkbook(ost);
			
			WritableSheet sheet=wwb.createSheet("sheet0", 0);
		
			Label l1 = new Label(0,0,"BO数据导入模板 ");
			sheet.mergeCells(0, 0, 2, 0);
			Label l2 = new Label(0,1,"产品名称");
			Label l3 = new Label(1,1,"产品编号");
			Label l4 = new Label(2,1,"初始图片数");
			
			sheet.addCell(l1);
			sheet.addCell(l2);
			sheet.addCell(l3);
			sheet.addCell(l4);
			
			wwb.write();
			wwb.close();
			in=new ByteArrayInputStream(ost.toByteArray());;
			ost.close();
	
		}catch(IOException e) {
			act.put("errorinfo", "I/O流出错！");
			in=null;
			e.printStackTrace();
		} catch (RowsExceededException e) {
			act.put("errorinfo", "excel表返回行数出错！");
			in=null;
			e.printStackTrace();
		} catch (WriteException e) {
			act.put("errorinfo", "excel写入出错！");
			in=null;
			e.printStackTrace();
		}catch (Exception e) {
			act.put("errorinfo", "excel写入出错！");
			in=null;
			e.printStackTrace();
		}
		return in;
	}
	
	/**
	 * 导出 市场调查 excel文档模板
	 * @return
	 */
	public InputStream getReportexcel(){
		
		ActionContext act= ServletActionContext.getContext();
		InputStream in=null;
		try{
			
			ByteArrayOutputStream ost=new ByteArrayOutputStream();
			WritableWorkbook  wwb = Workbook.createWorkbook(ost);
			
			WritableSheet sheet=wwb.createSheet("sheet0", 0);
		
			Label l1 = new Label(0,0,"今日礼品季刊导入模板 ");
			sheet.mergeCells(0, 0, 2, 0);
			Label l2 = new Label(0,1,"期数");
			Label l3 = new Label(1,1,"类别");
			Label l4 = new Label(2,1,"标题");
			Label l5 = new Label(3,1,"默认图片名");
			Label l6 = new Label(4,1,"初始图片数");
			Label l7 = new Label(5,1,"City");
			Label l8 = new Label(6,1,"Category");
			Label l9 = new Label(7,1,"Brand");
			Label l10 = new Label(8,1,"Est. cost w/ tax");
			Label l11 = new Label(9,1,"Promotion Mechanism");
			Label l12 = new Label(10,1,"Promotion Time");
			Label l13 = new Label(11,1,"Promotion Channel");
			Label l14 = new Label(12,1,"Discovery Location");
			Label l15 = new Label(13,1,"Premium Materials");
			
			
			sheet.addCell(l1);
			sheet.addCell(l2);
			sheet.addCell(l3);
			sheet.addCell(l4);
			sheet.addCell(l5);
			sheet.addCell(l6);
			sheet.addCell(l7);
			sheet.addCell(l8);
			sheet.addCell(l9);
			sheet.addCell(l10);
			sheet.addCell(l11);
			sheet.addCell(l12);
			sheet.addCell(l13);
			sheet.addCell(l14);
			sheet.addCell(l15);
			
			wwb.write();
			wwb.close();
			in=new ByteArrayInputStream(ost.toByteArray());;
			ost.close();
	
		}catch(IOException e) {
			act.put("errorinfo", "I/O流出错！");
			in=null;
			e.printStackTrace();
		} catch (RowsExceededException e) {
			act.put("errorinfo", "excel表返回行数出错！");
			in=null;
			e.printStackTrace();
		} catch (WriteException e) {
			act.put("errorinfo", "excel写入出错！");
			in=null;
			e.printStackTrace();
		}catch (Exception e) {
			act.put("errorinfo", "excel写入出错！");
			in=null;
			e.printStackTrace();
		}
		return in;
	}
	
	/**
	 * 获取期刊列表
	 * @return
	 */
	public String getBooksList(){
		
		pm.setCurrentPage(currentPage);
		pm.setPageSize(pageSize);

		List listOne = new ArrayList();
		String[] propertyName = { "kind","groupId","lable1" };
		String[] propertyCon = { "=","=","like" };
		Object[] propertyValue = { "book",qishu,keywords};
		String[] condition = {"and","and"};
		listOne.add(propertyName);
		listOne.add(propertyCon);
		listOne.add(propertyValue);
		listOne.add(condition);
		
		String[] orderPropertyName = { "creDate"};
		String[] orderValue = { "DESC"};

		pm = commonservice.findDataByPage(pm, "Photogroup", listOne, null, null, null, null, null, null, orderPropertyName, orderValue);

		return SUCCESS;
		
	}
	
	/**
	 * 获取BO列表
	 * @return
	 */
	public String getBoList(){
		
		pm.setCurrentPage(currentPage);
		
//		if("front".equals(act)){
//			pageSize = 8;
//			}
		
		pm.setPageSize(pageSize);


		List listOne = new ArrayList();
		String[] propertyName = { "kind" };
		String[] propertyCon = { "="};
		Object[] propertyValue = { "bo"};
		String[] condition = null;
		listOne.add(propertyName);
		listOne.add(propertyCon);
		listOne.add(propertyValue);
		listOne.add(condition);
		
		List listtwo = new ArrayList();
		String[] propertyName2 = { "lable1", "lable2"};
		String[] propertyCon2 = { "like", "like"};
		Object[] propertyValue2 = { keywords,keywords};
		String[] condition2 = {"or"};
		listtwo.add(propertyName2);
		listtwo.add(propertyCon2);
		listtwo.add(propertyValue2);
		listtwo.add(condition2);
		
		String[] orderPropertyName = { "turn"};
		String[] orderValue = { "asc"};

		pm = commonservice.findDataByPage(pm, "Photogroup", listOne, "and", listtwo, null, null, null, null, orderPropertyName, orderValue);
		if("front".equals(act)){
			return "front";
		}else{
			return SUCCESS;
		}
		
	}
	
	/**
	 * 后台列表下拉，AJAX查询期数
	 * @return
	 */
	public void loadqishu() throws Exception{
			String hql = "SELECT distinct pc.groupId, pc.groupName FROM Photogroup pc WHERE pc.kind = ? ORDER BY pc.groupId";
			Object[] param = {"book"};
			List list = findSomeByHQL.findDataByHql(hql, param);
			String jsonResult = this.changeListToStr(list);
			System.out.println(list.size()+"-------------");
			this.sendMsgAjax(jsonResult, null);
		}

	/**
	 * 删除期刊内容
	 * @return
	 */	
	public void deleteBooks() throws Exception{
		try{
			
			String[] ids = booksId.split(",");
			int length = ids.length;
			for(int i=0;i<length;i++){
				Photogroup p = commonservice.findById(ids[i], new Photogroup());
				commonservice.deleteDemo(p, null);
			}
			
			this.sendMsgAjax("success", null);
			
		}catch (Exception e) {
			e.printStackTrace();
			this.sendMsgAjax("error", null);
		}
	}
	
	/**
	 * 获取期刊内容
	 * @return
	 */	
	public String getBooks(){
	if(null != booksId){
		pg = commonservice.findById(booksId, new Photogroup());
		if("back".equals(act)){
				return "backupdate"; //返回报告编辑
		}else{
			String hql = "select af.saveName FROM Allfile af WHERE af.parentId = ? ORDER BY af.turn";
			Object[] param = {booksId};
			list = findSomeByHQL.findDataByHql(hql, param);
				pg.setLable19(pg.getLable19()+1);
				if("".equals(pg.getTurn())||null==pg.getTurn()){
					pg.setTurn(0);
				}
				commonservice.updateDemo(pg);
				//获取前后记录

				List<Photogroup> listpre = findSomeByHQL.findDataByHql("FROM Photogroup pr where pr.kind = 'book' and pr.groupTopId = '2' and pr.groupId = '"+pg.getGroupId()+"' and pr.turn < "+pg.getTurn()+" order by pr.turn desc");
				List<Photogroup> listnext = findSomeByHQL.findDataByHql("FROM Photogroup pr where pr.kind = 'book' and pr.groupTopId = '2' and pr.groupId = '"+pg.getGroupId()+"' and pr.turn > "+pg.getTurn()+" order by pr.turn asc");
				if(listpre.size()>0){
					System.out.println("有前");
					preid = listpre.get(0).getId();
					pretitle = listpre.get(0).getLable1();
				}else{
					preid = "none";
					pretitle = "none";
				}
				if(listnext.size()>0){
					System.out.println("有后");
					nextid = listnext.get(0).getId();
					nexttitle = listnext.get(0).getLable1();
				}else{
					nextid = "none";
					nexttitle = "none";
				}			
				return "frontget"; //返回报告
		}
	}else{
		errorinfo = "Time over";
		return LOGIN;
	}
	}
	
	/**
	 * 修改期刊内容
	 * @return
	 */
	public String updateBooks() throws Exception{
		
		String sadminId = (String)ActionContext.getContext().getSession().get("adminId");
		String sadminName = (String)ActionContext.getContext().getSession().get("adminName");
		if(null == sadminId){
			errorinfo = "Time Over";
			return LOGIN;
		}else{
			Photogroup p = commonservice.findById(booksId, new Photogroup());
			if("bo".equals(type)){
			p.setLable1(pg.getLable1());
			p.setLable2(pg.getLable2());
			}
			if("book".equals(type)&&!"2".equals(p.getGroupTopId())){
			p.setLable1(pg.getLable1());
			p.setLable21(pg.getLable21());
			}
			if("book".equals(type)&&"2".equals(p.getGroupTopId())){
				p.setLable1(pg.getLable1());
				p.setLable3(pg.getLable3());
				p.setLable4(pg.getLable4());
				p.setLable5(pg.getLable5());
				p.setLable6(pg.getLable6());
				p.setLable7(pg.getLable7());
				p.setLable8(pg.getLable8());
				p.setLable9(pg.getLable9());
				p.setLable10(pg.getLable10());
				p.setLable11(pg.getLable11());
				}		
			commonservice.updateDemo(p);
			if("bo".equals(type)){
				return SUCCESS;
			}else{
				return "book";
			}
		}
	}
	
	
	/**
	 * 前台期数调用
	 * @return
	 */
	public String getqishu() throws Exception{
			String hql = "SELECT distinct pc.groupId, pc.groupName FROM Photogroup pc WHERE pc.kind = ? ORDER BY pc.groupId";
			Object[] param = {"book"};
			list = findSomeByHQL.findDataByHql(hql, param);
			return SUCCESS;
		}
	
	
	/**
	 * 前台读取BO详情
	 * @return
	 */
	public String boDetail() throws Exception{
			pg = commonservice.findById(booksId, new Photogroup());
			pg.setLable19(pg.getLable19()+1);
			commonservice.updateDemo(pg);
			String hql = "SELECT pc.saveName FROM Allfile pc WHERE pc.parentId = ? ORDER BY pc.turn";
			Object[] param = {booksId};
			listpic = findSomeByHQL.findDataByHql(hql, param);
			return SUCCESS;
	}
	
	/**
	 * 前台读取期刊
	 * @return
	 */
	public String getbooksbyqishu() throws Exception{
		System.out.println(groupTopId);
		System.out.println(qishu);
			if("2".equals(groupTopId)){
				pm.setCurrentPage(currentPage);
				pm.setPageSize(pageSize);

				List listOne = new ArrayList();
				String[] propertyName = { "kind","groupId","groupTopId" };
				String[] propertyCon = { "=","=","="};
				Object[] propertyValue = { "book",qishu,"2"};
				String[] condition = {"and","and"};
				listOne.add(propertyName);
				listOne.add(propertyCon);
				listOne.add(propertyValue);
				listOne.add(condition);
				
				List listtwo = new ArrayList();
				String[] propertyName2 = { "lable1", "lable3", "lable4", "lable5", "lable6", "lable7", "lable8", "lable9", "lable10", "lable11"};
				String[] propertyCon2 = { "like", "like", "like", "like", "like", "like", "like", "like", "like", "like"};
				Object[] propertyValue2 = { keywords, keywords, keywords, keywords, keywords, keywords, keywords, keywords, keywords, keywords };
				String[] condition2 = {"or","or","or","or","or","or","or","or","or"};
				listtwo.add(propertyName2);
				listtwo.add(propertyCon2);
				listtwo.add(propertyValue2);
				listtwo.add(condition2);
				
				String[] orderPropertyName = { "creDate","turn"};
				String[] orderValue = { "DESC","ASC"};

				pm = commonservice.findDataByPage(pm, "Photogroup", listOne, "and", listtwo, null, null, null, null, orderPropertyName, orderValue);
				return "report";
			}else{
				List templist = findSomeByHQL.findDataByHql("SELECT p.id FROM Photogroup p where p.groupTopId = '"+groupTopId+"' and groupId = '"+qishu+"'");
				if(templist.size()>0){
				pg = commonservice.findById(templist.get(0), new Photogroup());
				String hql = "SELECT pc.saveName FROM Allfile pc WHERE pc.parentId = ? ORDER BY pc.turn";
				Object[] param = {pg.getId()};
				listpic = findSomeByHQL.findDataByHql(hql, param);
				pg.setLable19(pg.getLable19()+1);
				commonservice.updateDemo(pg);
				}else{
					System.out.println("木有数据");
				}
				return SUCCESS;
			}
	}
	
	
	/**
	 * 前台读取期刊
	 * @return
	 */
	public String getqishulist() throws Exception{
		String hql = "SELECT distinct pc.groupId, pc.groupName FROM Photogroup pc WHERE pc.kind = ? ORDER BY pc.groupId desc";
		Object[] param = {"book"};
		listpic = findSomeByHQL.findDataByHql(hql, param);
		totalqishu = listpic.size();
		return SUCCESS;
	}	
	
	public String exportbo(){
		return SUCCESS;
	}
	
	public String exportreport(){
		return SUCCESS;
	}

	public File getFile() {
		return file;
	}


	public void setFile(File file) {
		this.file = file;
	}


	public Photogroup getPg() {
		return pg;
	}


	public void setPg(Photogroup pg) {
		this.pg = pg;
	}


	public String getErrorinfo() {
		return errorinfo;
	}


	public void setErrorinfo(String errorinfo) {
		this.errorinfo = errorinfo;
	}


	public String getTesting() {
		return testing;
	}


	public void setTesting(String testing) {
		this.testing = testing;
	}


	public String getRows() {
		return rows;
	}


	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public PageModel getPm() {
		return pm;
	}

	public void setPm(PageModel pm) {
		this.pm = pm;
	}

	public String getQishu() {
		return qishu;
	}

	public void setQishu(String qishu) {
		this.qishu = qishu;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public String getBooksId() {
		return booksId;
	}

	public void setBooksId(String booksId) {
		this.booksId = booksId;
	}

	public String getGroupTopId() {
		return groupTopId;
	}

	public void setGroupTopId(String groupTopId) {
		this.groupTopId = groupTopId;
	}

	public String getAct() {
		return act;
	}

	public void setAct(String act) {
		this.act = act;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public List getListpic() {
		return listpic;
	}

	public void setListpic(List listpic) {
		this.listpic = listpic;
	}

	public int getTotalqishu() {
		return totalqishu;
	}

	public void setTotalqishu(int totalqishu) {
		this.totalqishu = totalqishu;
	}

	public String getPreid() {
		return preid;
	}

	public void setPreid(String preid) {
		this.preid = preid;
	}

	public String getNextid() {
		return nextid;
	}

	public void setNextid(String nextid) {
		this.nextid = nextid;
	}

	public String getPretitle() {
		return pretitle;
	}

	public void setPretitle(String pretitle) {
		this.pretitle = pretitle;
	}

	public String getNexttitle() {
		return nexttitle;
	}

	public void setNexttitle(String nexttitle) {
		this.nexttitle = nexttitle;
	}
	
	
}
